package com.mayo.feaf;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mayo.feaf.comun.BaseActivity;
import com.mayo.feaf.comun.TratamientoImagenComun;
import com.mayo.feaf.comun.gallery.TouchImageView;

public class MaximizarImagenActivity extends BaseActivity {

	private TouchImageView imagen;
	private ProgressBar progress;
	private TextView desc;

	private final String BUNDLE_URL = "url";
    private final String BUNDLE_BM = "bm";
    
	/** Se llama cuando la Activity es creada. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.max_image);
		
		actionBar.setTitle("Imagen");
		actionBar.setDisplayOptions(
				ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME
						| ActionBar.DISPLAY_HOME_AS_UP
						| ActionBar.DISPLAY_SHOW_CUSTOM);
		
		imagen = (TouchImageView) findViewById(R.id.image_max);
		progress = (ProgressBar) findViewById(R.id.progress);
//		imagen.setImageResource(R.drawable.playa1);
	    imagen.setMaxZoom(4f);

		Intent intent = getIntent();
		Bundle todosParametros = intent.getExtras();
		String urlImagen = todosParametros.getString("imagen");
		
		Bundle b = new Bundle();
        b.putString(BUNDLE_URL, urlImagen);
        
        new DownloadImagePrevia().execute(b);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
    
    public class DownloadImagePrevia extends AsyncTask<Bundle, Bundle, Bundle> {
		// Do the long-running work in here

		public Bundle doInBackground(Bundle... b) {
			Bitmap bm = null;
			try {
				//cache: for better performance, check if url alredy exists
	            	bm = TratamientoImagenComun.decodeSampledBitmapFromUri(b[0].getString(BUNDLE_URL), 
	            			getResources().getIntArray(R.array.grid_celda)[1], getResources().getIntArray(R.array.grid_celda)[1]);
	            Bundle bundle = new Bundle();
	            bundle.putParcelable(BUNDLE_BM, bm);

	            return bundle;
			} catch (NullPointerException e) {
			} catch (Exception e) {
			}
			return null;
		}

		// This is called when doInBackground() is finished
		public void onPostExecute(final Bundle result) {
			super.onPostExecute(result);
			 runOnUiThread(new Runnable() {
				 public void run() {
					 //get picture saved in the map + set
					 Bitmap bm = (Bitmap) result.getParcelable(BUNDLE_BM);
					 if (bm != null){ //if bitmap exists...
						 imagen.setImageBitmap(bm);
						 progress.setVisibility(ProgressBar.GONE);
						 imagen.setVisibility(ImageView.VISIBLE);
					 } else { //if not picture, display the default ressource
						 imagen.setImageResource(R.drawable.ic_launcher);
						 progress.setVisibility(ProgressBar.GONE);
						 imagen.setVisibility(ImageView.VISIBLE);
					 }
				 }
			 });
		}
	}
}
